package com.hsq.spzx.manager.controller;

import com.hsq.spzx.manager.service.SysMenuService;
import com.hsq.spzx.manager.service.ValidateCodeService;
import com.hsq.spzx.model.dto.system.LoginDto;
import com.hsq.spzx.model.entity.system.SysUser;
import com.hsq.spzx.model.vo.common.Result;
import com.hsq.spzx.model.vo.common.ResultCodeEnum;
import com.hsq.spzx.model.vo.system.LoginVo;
import com.hsq.spzx.manager.service.SysUserService;
import com.hsq.spzx.model.vo.system.SysMenuVo;
import com.hsq.spzx.model.vo.system.ValidateCodeVo;
import com.hsq.spzx.utils.AuthContextUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author hsq
 * @date 2025-05-26 23:41
 */

@Tag(name = "用户接口")
@RestController
@RequestMapping(value = "/admin/system/index")
public class IndexController {
    @Autowired
    private SysUserService sysUserService ;

    @Autowired
    private SysMenuService sysMenuService;

    @Autowired
    private ValidateCodeService validateCodeService;

    @Operation(summary = "登入方法")
    @PostMapping(value = "/login")
    public Result<LoginVo> login(@RequestBody LoginDto loginDto) {
        LoginVo loginVo = sysUserService.login(loginDto) ;
        return Result.build(loginVo , ResultCodeEnum.SUCCESS) ;
    }

    @Operation(summary = "获取验证码")
    @GetMapping(value = "/generateValidateCode")
    public Result<ValidateCodeVo> generateValidateCode(){
        ValidateCodeVo validateCodeVo = validateCodeService.generateValidateCode();
        return Result.build(validateCodeVo, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "获取用户信息")
    @GetMapping(value = "/getUserInfo")
    public Result getUserInfo(/*HttpServletRequest request*/
                                @RequestHeader(name = "token") String token) {
        //1.从请求头中获取token
//        String token = request.getHeader("token");
        //2.根据token查询redis获取用户信息
//        SysUser user = sysUserService.getUserInfo(token);
        //3.用户信息返回
        return Result.build(AuthContextUtil.get(), ResultCodeEnum.SUCCESS);
    }

    @GetMapping("/logout")
    public Result logout(@RequestHeader(name = "token") String token) {
        sysUserService.logout(token);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "根据用户显示菜单栏")
    @GetMapping("/menus")
    public Result<SysMenuVo> menus() {
        List<SysMenuVo> sysMenuVos = sysMenuService.findUserMenuList();
        return Result.build(sysMenuVos, ResultCodeEnum.SUCCESS);
    }
}
